import { _decorator } from "cc";
import { Component, Node } from "cc";
import { ResMgr } from "../manager/ResMgr";
import { Prefab } from "cc";
import { instantiate } from "cc";
import { director } from "cc";

const { ccclass, property } = _decorator;

/**
 * 公用Loading
 */
@ccclass('Loading')
export class Loading extends Component {
    private static _instance: Loading;
    public static async init() {
        let prefab = await ResMgr.instance.load("prefab/comm/Loading", Prefab);
        let node = instantiate(prefab);
        node.parent = director.getScene()!.getChildByPath('Canvas/Max');
        node.active = false;
        this._instance = node.getComponent(Loading);
    }
    public static show(sec: number = 0) {
        this._instance.show(sec);
    }
    public static hide() {
        this._instance.hide();
    }

    private show(sec: number = 0) {
        this.node.active = true;
        this.unscheduleAllCallbacks();
        if (sec > 0) {
            this.scheduleOnce(() => {
                this.hide();
            }, sec);
        }
    }
    private hide() {
        this.node.active = false;
    }
}